﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageSubAdmin : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_USERS = "__AdminUsers::DSU";
    private const string PROP_DATASRC_ROLES = "__AdminUsers::DSR";
    private const string PROP_LAYOUT = "__AdminUsers::UIL";
    private const string PROP_SELECTED_USER_ID = "__AdminUsers::SUID";
    private const string PROP_SELECTED_RETAILER_ID = "__AdminUsers::SRID";

    #endregion

    #region Public Properties

    public SubAdminCollection DataSourceUsers
    {
        get { return (SessionMgr.Session[PROP_DATASRC_USERS] is SubAdminCollection ? (SubAdminCollection)SessionMgr.Session[PROP_DATASRC_USERS] : (SubAdminCollection)(SessionMgr.Session[PROP_DATASRC_USERS] = new SubAdminCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_USERS] = value; }
    }

    public SubAdminRoleCollection DataSourceRoles
    {
        get { return (SessionMgr.Session[PROP_DATASRC_ROLES] is SubAdminRoleCollection ? (SubAdminRoleCollection)SessionMgr.Session[PROP_DATASRC_ROLES] : (SubAdminRoleCollection)(SessionMgr.Session[PROP_DATASRC_ROLES] = new SubAdminRoleCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_ROLES] = value; }
    }

    public int SelectedUserID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_USER_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_USER_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_USER_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceUsers = SubAdmin.GetAllUsers();
            SubAdminList1.DataBind(DataSourceUsers, Controls_SubAdminList.LayoutType.ListOnly);
        }
    }

    #endregion

    #region Control: SubAdminList

    protected void ucUserList_EditRequested(object sender, Controls_SubAdminList.EditRequestedEventArgs evt)
    {
        SelectedUserID = evt.User.UserID;
        DataSourceRoles = SubAdminRoleCollection.GetPermittedRolesByUserID(evt.User.UserID);
        SubAdminList1.SetSelectedUser(evt.User);
        SubAdminCreation1.DataBind(evt.User, DataSourceRoles, ViewEditMode.Edit);
        SetLayout(LayoutType.Edit);
    }


    protected void ucUserList_AddNewRequested(object sender, EventArgs evt)
    {
        SubAdminCreation1.CancelEdit();
        SubAdmin newUser = SubAdmin.CreateNew(DataSourceUsers);
        SelectedUserID = newUser.UserID;
        DataSourceRoles = SubAdminRoleCollection.GetPermittedRolesByUserID(newUser.UserID);
        SubAdminCreation1.DataBind(DataSourceUsers.FindByUserID(SelectedUserID), DataSourceRoles, ViewEditMode.Edit);
        SetLayout(LayoutType.Edit);
    }


    protected void ucUserList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceUsers = SubAdmin.GetAllUsers();
        SubAdminList1.DataSource = DataSourceUsers;
    }

    #endregion

    #region Control: SubAdminViewEdit

    protected void ucUserViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        SubAdminCreation1.DataSourceUser = DataSourceUsers.FindByUserID(SelectedUserID);
    }

    protected void ucUserViewEdit_DataSourceRolesRequested(object sender, EventArgs evt)
    {
        SubAdminCreation1.DataSourceRoles = DataSourceRoles;
    }

    #endregion

    #region Save / Cancel Buttons

    protected void bttncancel_Click(object sender, EventArgs e)
    {
        SubAdminCreation1.CancelEdit();
        // If we were adding a new user...then remove the incomplete/not added new user object from the collection.
        if (DataSourceUsers.FindByUserID(SelectedUserID).ObjState == BOBase.ObjectState.Added)
        {
            DataSourceUsers.Remove(DataSourceUsers.FindByUserID(SelectedUserID));
            SelectedUserID = (DataSourceUsers[0] != null ? DataSourceUsers[0].UserID : 0);
        }
        SubAdminList1.DataBind(DataSourceUsers, Controls_SubAdminList.LayoutType.ListOnly);
        SetLayout(LayoutType.View);

    }
    protected void bttnsave_Click(object sender, EventArgs e)
    {
        if (SubAdminCreation1.EndEdit())
        {
            SubAdmin obj = DataSourceUsers.FindByUserID(SelectedUserID);
            SubAdmin.Update(obj);

            // TODO: Set Feedback text...

            SubAdminList1.DataBind(DataSourceUsers, Controls_SubAdminList.LayoutType.ListOnly);
            SetLayout(LayoutType.View);

        }

    }

    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlSubAdminList.Visible = false;
                pnlSubAdminViewEdit.Visible = true;

                bttnsave.Visible = true;
                bttncancel.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlSubAdminList.Visible = true;
                pnlSubAdminViewEdit.Visible = false;

                bttnsave.Visible = false;
                bttncancel.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
